package com.china08.yxyapi.api.manage.sch.platform.live.catalog;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.db.live.YxyLiveCatalog;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.repository.db.live.YxyLiveCatalogRepository;

import io.swagger.annotations.ApiOperation;

@CrossOrigin
@RestController
public class LiveSchCatalogLessonCountController extends YxyApiBaseController {

    @Autowired
    private YxyLiveCatalogRepository catalogRepository;

    @Autowired
    JdbcTemplate jdbcTemplate;

    @ApiOperation(value = "")
    @RequestMapping(value = "/api_yw/manage/sch/platform/live/catalog/lesson/count", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public void precess(HttpServletRequest request, HttpServletResponse response, @RequestBody ReqModel reqModel) {
        String catalogId = reqModel.catalogId;
        String schoolId = getProfile(request, response).getSchoolId();
        // 判断有没有子栏目
        List<YxyLiveCatalog> lists = catalogRepository.findByPid(catalogId);
        Assert.isTrue(lists.size() == 0, "有子分类，请先删除子分类");

        /**
         * 判断有没有课程 删除的，草稿，未通过不算
         */
        StringBuffer sb = new StringBuffer();
        sb.append(" select                                                          ");
        sb.append("   count(id)                                                     ");
        sb.append(" from yxy_live.yxy_live_lesson                       ");
        sb.append(" where status !=  - 1                                            ");
        sb.append("       and status != 3                                           ");
        sb.append("       and status != 0                                           ");
        sb.append("       and (  catalog_id1 = ?                                    ");
        sb.append("           or catalog_id2 = ?                                    ");
        sb.append("           or catalog_id3 = ?                                  ");
        sb.append("             and catalog_school_id = ?)                     ");
        Integer count = jdbcTemplate.queryForObject(sb.toString(),
                new Object[] { catalogId, catalogId, catalogId, schoolId }, Integer.class);
        Assert.isTrue(count == 0, "该分类下面有课程，请先删除课程或转移课程再来删除分类");
    }

    public static class ReqModel extends YxyApiReqModel {
        public String catalogId;
        public Integer catalogLeave;

        @Override
        public void autoCheck() {

        }
    }
}
